﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using OddUserManager.Data.Repositories;
using OddUserManager.Service.IServices;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace OddUserManager.Web.Controllers
{
    [Authorize]

    public class BaseController : Controller
    {

        IMenusService _menusService;

        //public MicroBaseController() { }



       


        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            _menusService = new MenusService(new MenuRepository(), new AppUserRepository(), new RoleRepository());
            bool hasMenus = true;
            var controllerName = filterContext.RouteData.Values["controller"].ToString();
            var actionName = filterContext.RouteData.Values["action"].ToString();
            //在这里判断权限，权限不够的话 返回
            //....
            var userId = int.Parse(filterContext.HttpContext.User.Identity.Name);
            if (userId != 1)
            {
                //获取到用户的所有权限
                var menus = _menusService.GetMenusByUserId(userId);

               
                var childMenus = menus.Where(p => p.Code == controllerName).ToList();
                if (null == childMenus || childMenus.Count == 0)
                {
                    hasMenus = false;
                }


                if (actionName.Contains("Index") || actionName.Contains("Get"))
                {
                    if (childMenus.Count(p =>  p.Url.Contains("Index")) == 0)
                    {
                        hasMenus = false;
                    }
                }
                else if (actionName.Contains("Edit"))
                {
                    if (childMenus.Count(p =>  p.Url.Contains("Edit")) == 0)
                    {
                        hasMenus = false;
                    }
                }
                else if (actionName.Contains("Delete"))
                {
                    if (childMenus.Count(p => p.Url.Contains("Delete")) == 0)
                    {
                        hasMenus = false;
                    }
                }
            }
            if (hasMenus)
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                if (actionName.Contains("Edit") || actionName.Contains("Delete"))
                {
                    filterContext.Result = new ContentResult()
                    {
                        Content = "{'Result':Failed,'Message':'你没有权限,请联系管理员!'}"
                    };
                }
                else
                {

                    filterContext.Result = new ContentResult()
                    {
                        Content = "{'Result':Failed,'Message':'你没有权限,请联系管理员!'}"
                    };
                }
            }
        }

        /// <summary>
        /// 获取服务端验证的第一条错误信息
        /// </summary>
        /// <returns></returns>
        public string GetModelStateError()
        {
            foreach (var item in ModelState.Values)
            {
                if (item.Errors.Count > 0)
                {
                    return item.Errors[0].ErrorMessage;
                }
            }
            return "";
        }
    }
}
